//
//  JMSlider.m
//  JMControl
//
//  Created by Zhuangyh on 16/1/12.
//  Copyright © 2016年 dooya. All rights reserved.
//

#import "JMSlider.h"
@interface JMSlider ()
@property(nonatomic,assign)CGFloat startValue;
@property(nonatomic,assign)CGFloat endValue;
@end

@implementation JMSlider

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
         [self shareInit];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self shareInit];
    }
    return self;
}

- (void)shareInit{
    _widthOfSliderLine = 2;
    
}


//- (CGRect)maximumValueImageRectForBounds:(CGRect)bounds {
//    NSLog(@"%s:%f",__FUNCTION__,self.widthOfSliderLine);
//    CGRect rc = [super maximumValueImageRectForBounds:bounds];
//    rc.size.height = self.widthOfSliderLine;
//    rc.origin.y = bounds.size.height/2 - rc.size.height/2;
//    return rc;
//}
////
//- (CGRect)minimumValueImageRectForBounds:(CGRect)bounds {
//     NSLog(@"%s:%f",__FUNCTION__,self.widthOfSliderLine);
//    CGRect rc = [super minimumValueImageRectForBounds:bounds];
//    rc.size.height = self.widthOfSliderLine;
//    rc.origin.y = bounds.size.height/2 - rc.size.height/2;
//    return rc;
//}

- (CGRect)trackRectForBounds:(CGRect)bounds {
    if(_widthOfSliderLine == 0) {
        _widthOfSliderLine = 2;
    }
     NSLog(@"%s:%f",__FUNCTION__,self.widthOfSliderLine);
    CGRect rc = [super trackRectForBounds:bounds];
     NSLog(@"---%@----%@",NSStringFromCGRect(bounds),NSStringFromCGRect(rc));
    rc.size.height = self.widthOfSliderLine;
    rc.origin.y = bounds.size.height/2 - rc.size.height/2;
    return rc;
}
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value {
     NSLog(@"%s:%f",__FUNCTION__,self.widthOfSliderLine);
    rect.origin.x = rect.origin.x - 2 ;
    rect.size.width = rect.size.width + 4;
    return CGRectInset ([super thumbRectForBounds:bounds trackRect:rect value:value], 2 , 2);
}

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event {
    _startValue = self.value;
       NSLog(@"%s:%f",__FUNCTION__,self.value);
    BOOL retBool = [super beginTrackingWithTouch:touch withEvent:event];
    return retBool;
}
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event {
      NSLog(@"%s:%f",__FUNCTION__,self.value);
    BOOL retBool = [super continueTrackingWithTouch:touch withEvent:event];
    return retBool;
}
- (void)endTrackingWithTouch:(nullable UITouch *)touch withEvent:(nullable UIEvent *)event {
    _endValue = self.value;
     NSLog(@"%s:%f",__FUNCTION__,self.value);
    if (_onlyStopInStartAndEnd) {
        //阻止其发送UIControlEventValueChanged
        //    [super endTrackingWithTouch:touch withEvent:event];
        if (_endValue>_startValue){
            [self setValue:1 animated:YES];
            [ self sendActionsForControlEvents:UIControlEventValueChanged ];
        }else{
            [self setValue:0 animated:YES];
            [ self sendActionsForControlEvents:UIControlEventValueChanged ];
        }
    }else{
        [super endTrackingWithTouch:touch withEvent:event];
    }
}// touch is sometimes nil if cancelTracking calls through to this.

- (void)cancelTrackingWithEvent:(nullable UIEvent *)event {
    
}

#pragma mark-
//setmaximumTrackTintColor
@end
